package night;

import java.io.*;

public class 九二IOStream {
    public static void main(String[] args) {
//        readFile2();
//        readFile3();
        writeFile1();
    }

    public static void readFile1() {
        try {
            FileInputStream fis = new FileInputStream("D:/bb.txt");
            int s1 = fis.read();
            int s2 = fis.read();
            int s3 = fis.read();
            int s4 = fis.read();
            System.out.println(s1);
            System.out.println(s2);
            System.out.println(s3);
            System.out.println(s4);
            fis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void readFile2() {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("gg.txt");
            int temp = 0;
            while ((temp = fis.read()) != -1) {
                System.out.println((char) temp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != fis) {
                    fis.close();
                }
            } catch (Exception e) {

            }
        }
    }

    public static void readFile3() {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("gg.txt");
            byte[] car = new byte[10];
            int len = -1;
            while ((len = fis.read(car)) != -1) {
                System.out.println(new String(car, 0, len));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != fis) {
                    fis.close();
                }
            } catch (Exception e) {

            }
        }
    }

    public static void writeFile1() {
        OutputStream os = null;
        try {
            os = new FileOutputStream("dest.txt", true);
            String msg = "IO is so easy\r\n";
            byte[] dest = msg.getBytes();
            os.write(dest, 0, dest.length);
            os.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != os) {
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
